const express = require('express');
const loginMiddleware = require("../middleware/LoginMiddleware");

const GroupsDao = require('../database/GroupsDao')

const router = express.Router();

router.post('/', loginMiddleware, async (req, res) => {
    let user = req.user;
    let { group_name } = req.body;
    try {
        if (!group_name || group_name.trim() === '') {
            return res.send({ code: 4000, msg: "组名不能为空", data: null });
        }
        let result = await GroupsDao.addGroup(group_name, user.user_id);
        console.log("groups添加的结果/是:\n" +JSON.stringify(result) + "\n————\n");
        
        // 返回新创建的群组信息
        res.send({ 
            code: 2000, 
            msg: "添加成功", 
            data: { group_id: result.insertId } // 假设 result.insertId 是新群组的 ID
        });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "添加失败", data: null });
    }
})

router.delete('/:GroupsID', loginMiddleware, async (req, res) => {
    let GroupsID = req.params.GroupsID;
    try {
        let result = await GroupsDao.deleteGroup(GroupsID);
        console.log("groups删除的结果/:GroupsID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "删除成功", data: null });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "删除失败", data: null });
    }
})

router.get("/GroupsName", loginMiddleware, async (req, res) => {
    let GroupsName = req.query.group_name;
    let user = req.user;
    console.log("/GroupsName:\n" + GroupsName + "\n————\n");
    try {
        let result = await GroupsDao.selectGroupsByGroupNameAndUserID(GroupsName, user.user_id);
        console.log("groups查询的结果/GroupsName是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
    }
})

router.get("/page",loginMiddleware,async(req,res)=>{
    let {pageIndex,groupID,groupName}=req.query;
    
    try{
        let start = 5*(Number(pageIndex)-1);
        let pageObj = await GroupsDao.selectGroupsByPage(groupID,groupName,start,5);
        console.log("groups查询的结果/page是:\n" +JSON.stringify(pageObj) + "\n————\n");
        res.send({code:2000,msg:"查询成功",data:pageObj});
    }
    catch(err){
        console.log(err);
        res.send({code:5000,msg:"查询失败",data:null});
    }
})

router.get("/userId/:userId",loginMiddleware,async(req,res)=>{
    let userId = req.params.userId;
    try{
        let result = await GroupsDao.selectGroupsByUserID(userId);
        console.log("groups查询的结果/userId/:UserID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
    }
    catch(err){
        console.log(err);
        res.send({code:5000,msg:"查询失败",data:null});
    }
})

router.get('/:GroupsID', loginMiddleware, async (req, res) => {
    let GroupsID = req.params.GroupsID;
    try {
        let result = await GroupsDao.selectGroupsById(GroupsID);
        console.log("groups查询的结果/:GroupsID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
    }
})

router.get('/', loginMiddleware, async (req, res) => {
    let { group_name } = req.body;
    try {
        let result = await GroupsDao.selectGroupsByGroupName(group_name);
        console.log("groups查询的结果/是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "查询成功", data: result });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "查询失败", data: null });
    }
})



router.put('/:GroupsID', loginMiddleware, async (req, res) => {
    let GroupsID = req.params.GroupsID;
    let { group_name } = req.body;
    try {
        let result = await GroupsDao.updateGroup(GroupsID, group_name);
        console.log("groups修改的结果/:GroupsID是:\n" +JSON.stringify(result) + "\n————\n");
        res.send({ code: 2000, msg: "修改成功", data: null });
    }
    catch (err) {
        console.log(err);
        res.send({ code: 5000, msg: "修改失败", data: null });
    }
})

module.exports = router;